	/* 楼栋房间选择器
	 * 
	 * 选择小区，楼栋，房间时，去服务端获取相应数据，修改对应的select */

	var global_buildings="";
    var commSelect = null;
    var areaSelect = null;
    var buildingSelect = null;
    var unitSelect = null;
    var roomSelect = null;
    var ownerSelect = null;
    var devSelect = null;
    var gUrl = null;
    var ownerUrl = null;
    
    function setCommSelect(comm){
    	commSelect = comm;
    	$("select[name='"+commSelect+"'").css("width","150px");
    }
    
    function setAreaSelect(area){
    	areaSelect = area;
    }
    
    function setBuildingSelect(building){
    	buildingSelect = building;
    }
    
    function setUnitSelect(unit){
    	unitSelect = unit;
    }
    
    function setRoomSelect(room){
    	roomSelect = room;
    }
    
    function setDevSelect(dev){
    	devSelect = dev;
    }
    
    function setOwnerSelect(owner) {
    	ownerSelect = owner;
    }
    
    function setUrl(url){
    	gUrl = url;
    }
    
    function setOwnerUrl(url){
    	ownerUrl = url;
    }
    
    function resetSelect(selectName){
    	var select = $("select[name='"+selectName+"']");
    	var tempOption = "<option value=''>--请选择--</option>";
    	select.empty();
    	select.append(tempOption);
    }
    
    function appendZero(num){
    	if((typeof num) == "string"){
    		if(num.length == 1)
    			return "0"+num;
    		else
    			return num;
    	}
    	else if(parseInt(""+num)<10){
    		return "0"+parseInt(num);
    	}
    	else
    		return num;
    }
    
    function getSelectData(comm){
    	var commCodeStr = comm;
		var url = gUrl;
        var params = {
       		commCode:commCodeStr
        };

        $.post(
                url,        //服务器要接受的url
                params,     //传递的参数
                function cbf(data){ //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
                	var select = $("select[name='"+areaSelect+"']");
  					var old_area_name;
  					data.sort(function (a,b){
  						return a.areaName - b.areaName;
  					});
  					
  					global_buildings = data;
					for (var i = 0; i < data.length; i++) {
						if(old_area_name==data[i].areaName)
							continue;
						var areaDisp = data[i].areaAliasName == ''?appendZero(data[i].areaName) + "栋":data[i].areaAliasName;
						var tempOption = "<option value='" + appendZero(data[i].areaName) + "' data-aliasname='"+data[i].areaAliasName+"'>" + areaDisp + "</option>";
						select.append(tempOption);
						old_area_name = data[i].areaName;
					}
		},
		'json' //数据传递的类型  json
		);
    }
    
	function changeComm(){
		resetSelect(areaSelect);
		resetSelect(unitSelect);
		resetSelect(buildingSelect);
		resetSelect(roomSelect);
		getSelectData($("select[name='"+commSelect+"']").val());
	}

	/* 选择小区时同时确定该小区是否有pstn服务器，只给业主模块使用 */
    function changeCommOwner(){
        resetSelect(areaSelect);
        resetSelect(unitSelect);
        resetSelect(buildingSelect);
        resetSelect(roomSelect);
        var comm = $("select[name='"+commSelect+"']").val();
        getSelectData(comm);

        var url = "/comm/getCommInfo.x";
        var params = {
            commCode:comm,
			type:"allowPstn"
        };

        $.post(
            url,        //服务器要接受的url
            params,     //传递的参数
            function cbf(data){ //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
				console.log(data);
                if(data.data == true){
                	$("#pstnSelect").css("display","inline-block");
				}else{
                    $("#pstnSelect").hide();
				}
            },
            'json' //数据传递的类型  json
        );
    }
	
	function changeArea(){
	
		resetSelect(unitSelect);
		resetSelect(buildingSelect);
		resetSelect(roomSelect);
		
		var select = $("select[name='"+buildingSelect+"']");
		var areaSeleted = $("select[name='"+areaSelect+"']").val();
		var buildingArray = [];
		var j=0;
		for (var i = 0; i < global_buildings.length; i++) {
			if(areaSeleted != appendZero(global_buildings[i].areaName))
				continue;
			buildingArray[j]=appendZero(global_buildings[i].buildingName);
			j++;
		}
		
		buildingArray.sort();
		
		for(var i=0; i < buildingArray.length; i++) {
			var tempOption = "<option value=\"" + appendZero(buildingArray[i]) + "\">" + appendZero(buildingArray[i]) + "单元</option>";
			select.append(tempOption);
		}
	}
	
	/* 改变building select时，动态改变后面的楼层和房号的select */
	function changeBuilding(){	
		resetSelect(unitSelect);
		resetSelect(roomSelect);
		
		var areaCodeStr = $("select[name='"+areaSelect+"']").val();
		var buildingCodeStr = $("select[name='"+buildingSelect+"']").val();
		var unitNum = 0;
		var roomNum = 0;
		for(var i=0; i< global_buildings.length;i++){
			if(areaCodeStr == appendZero(global_buildings[i].areaName) && buildingCodeStr == appendZero(global_buildings[i].buildingName)){
				unitNum = global_buildings[i].unitNumber;
				roomNum = global_buildings[i].roomNumber;
				break;
			}
		}

		var select = $("select[name='"+unitSelect+"']");
		for (var i = 1; unitNum!=0 && i <= unitNum; i++) {
			var tempOption = "<option value=\"" + appendZero(i) + "\">" + appendZero(i) + "楼</option>";
			select.append(tempOption);
		}
		
		if(roomSelect==null)
			return;
		var select = $("select[name='"+roomSelect+"']");
		for (var i = 1; roomNum!=0 && i <= roomNum; i++) {
			var tempOption = "<option value=\"" + appendZero(i) + "\">" + appendZero(i) + "房间</option>";
			select.append(tempOption);
		}
		
	}
	
	/* 改变building select时，改变设备号select,门禁卡模块使用 
	    选择设备号的同时，计算出这个设备号在整个小区所有楼栋单元的设备中的牌号*/
	function changeBuildingDev(){
		resetSelect(devSelect);
		var commCodeStr = $("select[name='"+commSelect+"']").val();
		var areaCodeStr = $("select[name='"+areaSelect+"']").val();
		var buildingCodeStr = $("select[name='"+buildingSelect+"']").val();
		var devNum = 0;
		var devInAll = 0;
		/* 先计算出当前单元之前所有楼栋的设备数量 ,由于global_buildings不一定良好排序，因此需要遍历所有对象 */
		for(var i=0; i< global_buildings.length;i++){		
			
			if((parseInt(global_buildings[i].areaName) < parseInt(areaCodeStr)) || 
					(parseInt(global_buildings[i].areaName) == parseInt(areaCodeStr) && 
							parseInt(global_buildings[i].buildingName) < parseInt(buildingCodeStr)))
			{
				devInAll += global_buildings[i].devNumber;
			}
			else if(parseInt(global_buildings[i].areaName) == parseInt(areaCodeStr) && 
					parseInt(global_buildings[i].buildingName) == parseInt(buildingCodeStr)){		
				devNum = global_buildings[i].devNumber;
				console.log("devNum "+devNum);
			}
			
		}
		
		
		var select = $("select[name='"+devSelect+"']");
		for (var i = 1; devNum!=0 && i <= devNum; i++) {
			var tempOption = "<option name='"+ appendZero(i)+"' value=\"" + commCodeStr + areaCodeStr + buildingCodeStr + "00" + appendZero(i) + "\">" + appendZero(i) + "</option>";
			select.append(tempOption);
		}
		
		console.log("devInAll "+devInAll);
	}
	
	/* Room Select 的onChange回调， 回调时, 获取该房间的业主信息，并更新相应的select */
	function changeRoomOwner() {
		var comm = $("select[name='"+commSelect+"']").val();
		var area = $("select[name='"+areaSelect+"']").val();
		var building = $("select[name='"+buildingSelect+"']").val();
		var floor = $("select[name='"+unitSelect+"']").val();
		var room = $("select[name='"+roomSelect+"']").val();
		
		var roomInfo = comm+area+building+floor+room;
		var url = ownerUrl;
        var params = {
       		roomCode:roomInfo
        };

        resetSelect(ownerSelect);

        $.post(
                url,        //服务器要接受的url
                params,     //传递的参数
                function cbf(data){ //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
                	var select = $("select[name='"+ownerSelect+"']");
                	console.log(data);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
					for (var i = 0; i < data.data.length; i++) {
						var tempOption = "<option name=\""+roomInfo+"\" value=\"" + appendZero(data.data[i].ownerCode) + "\">" + appendZero(data.data[i].ownerName) + "</option>";
						select.append(tempOption);
					}
		},
		'json' //数据传递的类型  json
		);
	}
	